package com.omg.server.crawler.strategy.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.omg.business.model.PostMeta;
import com.omg.business.model.Posts;
import com.omg.business.model.Terms;
import com.omg.business.model.TermsRelation;
import com.omg.business.model.TermsTaxonomy;
import com.omg.server.crawler.RequestContext;
import com.omg.server.crawler.strategy.CrawlerStrategy;
import com.omg.server.framework.service.IDefaultService;
import com.omg.server.util.CommonUtil;
import com.omg.server.util.DateUtil;
import com.omg.weibo.WeiBoSender;
/**
 * 买个便宜货 美国便宜货 网站抓取策略
 * @author Administrator
 */
public class FeiMaiBookStrategyImpl implements CrawlerStrategy{
	private static final Log log = LogFactory.getLog(SmzdmStrategyImpl.class);
	private IDefaultService defaultService;
	@Override
	public void handle(RequestContext requestContext) {
		int pageIndex = requestContext.getStartIndex();
		if(pageIndex < 1){
			return;
		}
		String url = requestContext.getSiteUrl();
		String soureUrl = requestContext.getSiteUrl();
		if (CommonUtil.isEmpty(url)){
			return;
		}
		if (pageIndex > 1){
			url +="page/"+pageIndex;
		}
		Source source = null;
		try {
			String subStract = null;
			String imgUrl = null;
			String imgUrl2 = null;
			String targetUrl = null;
			Element subElement = null;
			List<Element> linkList = null;
			String linkContent = null;
			Element subElement2 = null;
			String priceInfo = null;
			String issueTime = null;
			String type = null;
			String tag = null;
			Posts posts = null;
			PostMeta postMeta = null;
			PostMeta postMeta2 = null;
			List<Element> bizList = null;
			String text = null;
			String[] txtValues = null;
			source = new Source(new URL(url));
			// Call fullSequentialParse manually as most of the source will be parsed.
			source.fullSequentialParse();
			String price = null;
			String revPrice = null;
			String title = null;
			int mIndex = -1;
			int spaceIndex = -1;
			Element rootElement = source.getElementById("post_list");
			Date issueDate = null;
			List<Integer> count = null;
			Map<String, Object> params = new HashMap<String, Object>(2);
			HashSet<PostMeta> pmSet = new HashSet<PostMeta>(2);
			if (null != rootElement){
				WeiBoSender wbs = new WeiBoSender();
				List<Element> goodList = rootElement.getAllElements(HTMLElementName.LI);
				if(CommonUtil.isNotEmpty(goodList)){
					for (Element el: goodList){
						try{
							title = null;
							price = null;
							subStract = null;
							targetUrl = null;
							issueDate = null;
							type = null;
							imgUrl = null;
							imgUrl2 = null;
							issueTime = null;
							posts = new Posts();
							postMeta = new PostMeta();
							postMeta2 = new PostMeta();
							subElement = el.getFirstElement(HTMLElementName.A);
							if (null != subElement){
							    priceInfo = subElement.getContent().toString();//价格信息
							    spaceIndex = priceInfo.indexOf("美元");
							    if (spaceIndex < 0){
							    	mIndex =  priceInfo.indexOf("元");
							    }
							    else{
							    	mIndex = spaceIndex;
							    }
								if (mIndex > -1){
									
									price = "";
									for (int j=mIndex-1; mIndex > 0; j--){
									    if ((priceInfo.charAt(j) <= '9' && priceInfo.charAt(j) >= '0') || priceInfo.charAt(j) == '.'){
										    price += priceInfo.substring(j, j + 1);
								        }
									    else{
									    	break;
									    }
								    }
									if (null != price){
										revPrice = "";
									    for(int i=price.length()-1; i > -1; i--){
									    	revPrice += price.charAt(i);
									    }
									}
									if (spaceIndex > -1){
										revPrice += "美元";
									}
									else{
										revPrice += "元";
									}
									title = priceInfo.replace(revPrice, "");
								}
							}
							bizList = el.getAllElementsByClass("meta");
							if (null != bizList && bizList.size() > 1){
							    issueTime = bizList.get(0).getTextExtractor().toString();//发布时间
							    text = bizList.get(1).getTextExtractor().toString();
							    if (CommonUtil.isNotEmpty(text)){
							    	txtValues = text.split("\\|");
							    	if (txtValues.length > 2){
							    		if (null != txtValues[1] && txtValues[1].split("：").length > 1){
							    		    type = txtValues[1].split("：")[1]; //商品类型
							    		}
							    		else{
							    			type = "";
							    		}
							    		if (null != txtValues[2] && txtValues[2].split("：").length > 1){
							    		    tag = txtValues[2].split("：")[1]; //标签
							    		}
							    		else{
							    			tag = "";
							    		}
							    	}
							    }
							}
							subElement =  el.getFirstElementByClass("excerpt");
							if (null != subElement){
								subElement2 = subElement.getFirstElement(HTMLElementName.A);
								if (null != subElement2){
									targetUrl = subElement2.getAttributeValue("href");  //商品目标地址
								}
								subStract = subElement.getContent().toString();//商品摘要
								linkList = subElement.getAllElements(HTMLElementName.A);
								if (CommonUtil.isNotEmpty(linkList) && null != subStract){
									for (Element cl : linkList){
										linkContent = cl.getTextExtractor().toString();
										subStract = subStract.replace(cl.toString(), linkContent);
									}
								}
								subStract.replaceAll(soureUrl, "www.buymefan.com");
								
								subElement = subElement.getFirstElement(HTMLElementName.IMG);
								if (null != subElement){
									imgUrl = subElement.getAttributeValue("src");//商品图片位置
									/*if (null !=imgUrl && imgUrl.indexOf(soureUrl) < 0){
										if (imgUrl.startsWith("/")){
										   imgUrl = soureUrl.substring(0, soureUrl.length()-1) + imgUrl;
										}
										else{
											imgUrl = soureUrl + imgUrl;
										}
									}*/
								}
								List<Element> imgsList = subElement.getAllElements(HTMLElementName.IMG);
								if(null != imgsList)
								for (Element e : imgsList){
									if (CommonUtil.isEmpty(imgUrl2)){
										imgUrl2 = e.getAttributeValue("src");
										
									}
									subStract = subStract.replace(e.toString(), "");
								}
							}
							if (CommonUtil.isNotEmpty(issueTime)){
								issueTime =issueTime.trim();
							}
							else{
								continue;
							}
							issueTime = issueTime + " " + DateUtil.formatDate(new Date(), DateUtil.HHMM);
							issueDate = DateUtil.convertToDate(issueTime, DateUtil.PATTERN_NOSECOND);
							if (null != requestContext.getMaxDate() && null != issueDate){
							    if (issueDate.before(requestContext.getMaxDate())){
								    continue;
							    }
							}
							targetUrl = CommonUtil.getFinalUrl(targetUrl);
							if (null != targetUrl){
								if (targetUrl.contains(soureUrl)){
									continue;
								}
								if (targetUrl.startsWith("ttp://")){
									targetUrl = "h" + targetUrl;
								}
							   // targetUrl = URLDecoder.decode(targetUrl);
							}
							//检查库中是否有这样的数据
							params.put("title", title);
							params.put("soureUrl", soureUrl);
							params.put("targetUrl", targetUrl);
							count = defaultService.find_iBatis("omg.crawler.checkGoodsIsExist", params);
							if (CommonUtil.isNotEmpty(count)){
								if (count.get(0) > 0){
									continue;
								}
							}
							postMeta2.setMetaKey("targeturl");
							postMeta2.setMetaValue(targetUrl);
							postMeta.setMetaKey("thumbnail");
							if(CommonUtil.isEmpty(imgUrl)){
								imgUrl = imgUrl2;
							}
							postMeta.setMetaValue(imgUrl);
							//postMeta.setId(UUID.randomUUID().toString());
							//postMeta2.setId(UUID.randomUUID().toString());
							
							posts.setSourceUrl(soureUrl);
							posts.setPostContent(subStract);
							posts.setPostName(title);
							posts.setPostDate(issueDate);
							posts.setPostDate2(issueDate);
							posts.setPostModified(issueDate);
							posts.setPostModified2(issueDate);
							posts.setPostTitle(title);
							if(null != price && price.matches("[\\d.]+") && price.indexOf("元") < 0){
								price += "元";
							}
							posts.setPrice(price);
						
							//int postId = 0;
							if (CommonUtil.isNotEmpty(title) && CommonUtil.isNotEmpty(subStract) && CommonUtil.isNotEmpty(targetUrl)){
								pmSet.add(postMeta);
								pmSet.add(postMeta2);
								//posts.setPostMetas(pmSet);
								WriteDBUtil.writeToDb(posts, pmSet, type, tag, "、", "、", imgUrl, wbs);
								pmSet.clear(); //清空扩展
								/*
							    defaultService.save_Hibernate_Object(posts);
							    pmSet.clear(); //清空扩展
							    postId = posts.getId();
							    if (postId <= 0){
							    	log.debug("生成的post_id 为:" + postId);
							    }
							    this.saveCataAndTag(type, tag, postId);
							    log.info(url + "商品录入成功.");
							    
							    if ("1".equals(PropertiesHelper.getProperty(ISSENDWEIBO))){
							        //发送微博
							        wbs.send(title, imgUrl, CrawlerStrategy.SITEURL+postId);
							    }*/
							}
						 }
					    catch(Exception e){
						    e.printStackTrace();
					    }
						finally{
							pmSet.clear(); //清空扩展
						}
					}
				}
				
			}
		}
		catch (MalformedURLException e) {
			log.error(e);
		} 
		catch (IOException e) {
			log.error(e);
		}
	}
	
	 private void saveCataAndTag(String type, String tag, int postId){
	    	Map<String, String> params = new HashMap<String,String>();
	    	List<Integer> termTaxonomy = null;
	    	int cataTermTaxonomyId = -1;
	    	int tagTermTaxonomyId = -1;
	    	
	    	if (CommonUtil.isEmpty(type)){
				type = "其它";
			}
			else{
				type = type.trim();
			}
			
			
			String[] types = type.split("、");
			TermsRelation termsRelation = null;
			TermsTaxonomy termsTaxonomy = null;
			Terms terms = null;
			for (int i=0, len=types.length; i < len; i++){
				cataTermTaxonomyId = -1;
				//检查目录是否存在
				params.clear();
				params.put("taxonomy", "category");
				params.put("termName", types[i]);
				termTaxonomy = defaultService.find_iBatis("omg.post.checkTermsIsExist", params);
				if (CommonUtil.isNotEmpty(termTaxonomy)){
					cataTermTaxonomyId = termTaxonomy.get(0);
				}
				
				//保存分类目录
				termsRelation = new TermsRelation();
				if (-1 == cataTermTaxonomyId){
					terms = new Terms();
					terms.setName(types[i]);
					terms.setSlug(URLEncoder.encode(types[i]));
					defaultService.save_Hibernate_Object(terms);
					termsTaxonomy = new TermsTaxonomy();
					termsTaxonomy.setTermId(terms.getId());
					termsTaxonomy.setTaxonomy("category");
					termsTaxonomy.setDescription("商品分类目录");
					defaultService.save_Hibernate_Object(termsTaxonomy);
					cataTermTaxonomyId = termsTaxonomy.getId();
				}
				termsRelation.setId(postId);
				termsRelation.setTermOrder(i);
				termsRelation.setTermTaxonomyId(cataTermTaxonomyId);
				defaultService.save_Hibernate_Object(termsRelation);
				log.debug("商品分类目录成功:" +postId);
			}
			
			if (CommonUtil.isNotEmpty(tag)){
				String[] tags = tag.split("、");
				for (int i=0,len=tags.length;i < len; i++){
					
					tagTermTaxonomyId = -1;
					//检查标签是否已存在
					params.clear();
					params.put("taxonomy", "post_tag");
					params.put("termName", tags[i]);
					termTaxonomy = defaultService.find_iBatis("omg.post.checkTermsIsExist", params);
					if (CommonUtil.isNotEmpty(termTaxonomy)){
						tagTermTaxonomyId = termTaxonomy.get(0);
					}
					try{
						//保存标签
						termsRelation = new TermsRelation();
						if (-1 == tagTermTaxonomyId){
						   terms = new Terms();
						   terms.setName(tags[i]);
						   terms.setSlug(URLEncoder.encode(tags[i]));
						   defaultService.save_Hibernate_Object(terms);
						   termsTaxonomy = new TermsTaxonomy();
						   termsTaxonomy.setTermId(terms.getId());
						   termsTaxonomy.setTaxonomy("post_tag");
						   termsTaxonomy.setDescription("商品标签");
						   defaultService.save_Hibernate_Object(termsTaxonomy);
						   tagTermTaxonomyId = termsTaxonomy.getId();
						}
						termsRelation.setId(postId);
						termsRelation.setTermOrder(i);
						termsRelation.setTermTaxonomyId(tagTermTaxonomyId);
						defaultService.save_Hibernate_Object(termsRelation);
						log.debug("商品标签成功:" +postId);
					}
					catch(Exception e){
						e.printStackTrace();
					}
				}
			}
			
	}
	public IDefaultService getDefaultService() {
		return defaultService;
	}
	public void setDefaultService(IDefaultService defaultService) {
		this.defaultService = defaultService;
	}
	
	public static void main(String[] args){
		RequestContext requestContext = new RequestContext();
		requestContext.setStartIndex(1);
		requestContext.setSiteUrl("http://www.feimaibook.com/");
		FeiMaiBookStrategyImpl impl = new FeiMaiBookStrategyImpl();
		for (int i=1; i < 15; i ++){
		   requestContext.setStartIndex(i);
		   impl.handle(requestContext);
		}
	}

}
